package com.osmino.contacts.dialer;

import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.osmino.contacts.R;
import com.osmino.core.style.IStyleableView;
import com.osmino.core.style.Style;
import com.osmino.core.utils.view.Backgroung;

public class Dialer extends LinearLayout implements IStyleableView, OnClickListener,
		OnLongClickListener {

	public static final String NAME = "dialer";
	private boolean DBG = true;
	private static final int LAYOUT = R.layout.dialer;
	private static final int ID_INPUT_BG = R.id.dialer_input_ll;
	private static final int ID_INPUT_FIELD = R.id.dialer_input_tv;
	private static final int[] sButtonsIds = { R.id.dialer_1_ib, R.id.dialer_2_ib,
			R.id.dialer_3_ib, R.id.dialer_4_ib, R.id.dialer_5_ib, R.id.dialer_6_ib,
			R.id.dialer_7_ib, R.id.dialer_8_ib, R.id.dialer_9_ib, R.id.dialer_0_ib,
			R.id.dialer_star_ib, R.id.dialer_n_ib, R.id.dialer_call_ib, R.id.dialer_bs_ib };
	private final TextView mInput;
	private static final String mChars = " 1234567890#*+";
	private DialerClient mClient;
	private LinearLayout root;
	private boolean isCross = false;

	public Dialer(Activity activity, DialerClient client, Style style) {
		super(activity);
		root = (LinearLayout) activity.getLayoutInflater().inflate(LAYOUT, this);
		for (int i = 0; i < sButtonsIds.length; i++) {
			ImageButton ib = (ImageButton) findViewById(sButtonsIds[i]);
			ib.setOnClickListener(this);
			ib.setOnLongClickListener(this);
		}
		ImageButton call = (ImageButton) findViewById(R.id.dialer_call_ib);
		Backgroung.setResource(call, R.drawable.dialer_call_ic,
				R.drawable.dialer_call_ic_act);// TODO FIX ALL RES
		setClient(client);
		mInput = (TextView) findViewById(ID_INPUT_FIELD);
		mInput.addTextChangedListener(onTextChanged);
	}

	TextWatcher onTextChanged = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			mClient.onTextChanged(mInput.getText().toString());
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}

		@Override
		public void afterTextChanged(Editable s) {
		}
	};

	@Override
	public void setStyle(Style style) {
	}

	@Override
	public void onClick(View view) {
		int id = view.getId();
		if (isCross && id == R.id.dialer_0_ib) {
			isCross = false;
			return;
		}
		processClick(id);
		isCross = false;
	}

	@Override
	public boolean onLongClick(View view) {
		int id = view.getId();
		switch (id) {
			case R.id.dialer_0_ib:
				mInput.setText(mInput.getText().toString() + mChars.charAt(13));
				isCross = true;
				break;
			case R.id.dialer_bs_ib:
				mInput.setText("");
				break;
		}
		return false;
	}

	// String url = "tel:3334444";
	// Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
	private void processClick(int id) {
		switch (id) {
			case R.id.dialer_1_ib:
				mInput.setText(mInput.getText().toString() + mChars.charAt(1));
				break;
			case R.id.dialer_2_ib:
				mInput.setText(mInput.getText().toString() + mChars.charAt(2));
				break;
			case R.id.dialer_3_ib:
				mInput.setText(mInput.getText().toString() + mChars.charAt(3));
				break;
			case R.id.dialer_4_ib:
				mInput.setText(mInput.getText().toString() + mChars.charAt(4));
				break;
			case R.id.dialer_5_ib:
				mInput.setText(mInput.getText().toString() + mChars.charAt(5));
				break;
			case R.id.dialer_6_ib:
				mInput.setText(mInput.getText().toString() + mChars.charAt(6));
				break;
			case R.id.dialer_7_ib:
				mInput.setText(mInput.getText().toString() + mChars.charAt(7));
				break;
			case R.id.dialer_8_ib:
				mInput.setText(mInput.getText().toString() + mChars.charAt(8));
				break;
			case R.id.dialer_9_ib:
				mInput.setText(mInput.getText().toString() + mChars.charAt(9));
				break;
			case R.id.dialer_0_ib:
				mInput.setText(mInput.getText().toString() + mChars.charAt(10));
				break;
			case R.id.dialer_star_ib:
				mInput.setText(mInput.getText().toString() + mChars.charAt(11));
				break;
			case R.id.dialer_n_ib:
				mInput.setText(mInput.getText().toString() + mChars.charAt(12));
				break;
			case R.id.dialer_call_ib:
				mClient.onCall(mInput.getText().toString());
				break;
			case R.id.dialer_bs_ib:
				String text = mInput.getText().toString();
				int len = text.length();
				String trimEnd = text.length() > 0 ? text.substring(0, len - 1) : "";
				mInput.setText(trimEnd);
				break;
		}
		mClient.onDialerButtonClick();
	}
	
	public String getText() {
		return mInput.getText().toString();
	}

	public interface DialerClient {

		public void onTextChanged(String string);

		public void onCall(String phoneNumber);
		
		public void onDialerButtonClick();
	}

	public DialerClient getClient() {
		return mClient;
	}

	public void setClient(DialerClient mClient) {
		this.mClient = mClient;
	}
}